load("//tools/lint:lint.bzl", "add_lint_tests")

package(default_visibility = ["//visibility:public"])

config_setting(
    name = "apple",
    constraint_values = ["@platforms//os:osx"],
)

config_setting(
    name = "apple_debug",
    constraint_values = ["@platforms//os:osx"],
    values = {"compilation_mode": "dbg"},
)

config_setting(
    name = "debug",
    values = {"compilation_mode": "dbg"},
)

config_setting(
    name = "linux",
    constraint_values = ["@platforms//os:linux"],
)

filegroup(
    name = "toolchain_deps",
    data = select({
        ":apple": ["@local_config_cc//:cc_wrapper"],
        "//conditions:default": [],
    }),
    visibility = ["//common:__pkg__"],
)

# Capture some bazel cc_toolchain variables into a bash environment file. These
# values reflect the compiler and flags that Bazel's compilation actions will
# use. Note that this might refer to Bazel's compiler wrapper, `cc_wrapper.sh`,
# and thus may only indirectly identify the actual compiler in use. Note also
# that `$(CC)` may refer to the canonicalized location of the compiler; for
# example, on Ubuntu, Clang may be `/usr/lib/llvm-<version>/bin/clang`,
# canonicalized from `/usr/bin/clang-<version>`.
genrule(
    name = "capture_cc",
    outs = ["capture_cc.env"],
    cmd = "cat <<EOF > '$@'\n" +
          "BAZEL_CC=\"$(CC)\"\n" +
          "BAZEL_CC_FLAGS=\"$(CC_FLAGS)\"\n" +
          "EOF",
    toolchains = [
        "@bazel_tools//tools/cpp:cc_flags",
        "@bazel_tools//tools/cpp:current_cc_toolchain",
    ],
    visibility = ["//common:__pkg__"],
)

# Utility script to indicate which compiler is being used.
sh_binary(
    name = "print_host_settings",
    srcs = ["print_host_settings.sh"],
    args = ["$(location :capture_cc.env)"],
    data = [
        ":capture_cc.env",
        ":toolchain_deps",
    ],
)

# Test the above script; if CROSSTOOL is removed or changed, $(CC) may be an
# invalid binary (related to #7763), and CI should catch this.
# N.B. We have to use a proxy script since `print_host_settings` has `data`.
sh_test(
    name = "print_host_settings_test",
    srcs = ["test/print_host_settings_test.sh"],
    data = [":print_host_settings"],
    # Valgrind Memcheck reports numerous leaks in the python executable.
    tags = ["no_memcheck"],
)

# Capture bazel's action environment variables into a bash environment file.
# These values reflect ONLY the command-line (or rcfile) `--action_env`
# settings that Bazel uses when running its toolchain configuration, e.g.
# `CC=clang`. This is not necessarily the same as what compiler (or compiler
# wrapper) is actually invoked during a build. Beware that environment
# variables passed like `CC=... bazel ...` are NOT captured by this!
genrule(
    name = "capture_compiler_config",
    outs = ["capture_compiler_config.env"],
    cmd = "cat <<EOF > '$@'\n" +
          "PATH=\"$$PATH\"\n" +
          "CC=\"$${CC:-}\"\n" +
          "CXX=\"$${CXX:-}\"\n" +
          "EOF",
    visibility = ["//common:__pkg__"],
)

# Utility script to indicate which compiler is selected by use of
# `--config={gcc,clang}`. This is intended to be used by the CI infrastructure,
# which needs a way to get this specific information. Unless you are very sure
# you understand the first sentence of this comment, as well as the caveats
# noted in the comment on the above `genrule`, DON'T USE THIS.
sh_binary(
    name = "print_compiler_config",
    srcs = ["print_compiler_config.sh"],
    args = ["$(location :capture_compiler_config.env)"],
    data = [
        ":capture_compiler_config.env",
    ],
)

add_lint_tests()
